// pages/receive-gift/receive-gift.js
var util = require("../../utils/util.js");
const innerAudioContext = wx.createInnerAudioContext();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isPassword: !1,
        isAddress: !1,
        giveType: '',
        orderId: '',
        region: ['请选择地区'],
        pickerColor: '#818181',
        isReceived: !1,
        showTime1: '00:00',
        showTime2: '00:00',
        audiosrc: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const _this = this;
        // _this.getGiftInfo('061115503670379');
        // _this.getCradUrl('34');
        console.log(options);
        if (options.orderId) {
            _this.getGiftInfo(options.orderId);
            _this.setData({
                orderId: options.orderId
            })
        }
        if (options.giveType) {
            _this.setData({
                giveType: options.giveType
            })
        }
        if (options.cardId) {
            _this.getCradUrl(options.cardId);
        }
    },
    toReceive: function() {
        const _this = this;
        if (!_this.data.command) {
            wx.showToast({
                title: '请填写口令', //提示的内容,
                icon: "none", //图标,
                //duration: 1500, //延迟时间,
                mask: false, //显示透明蒙层，防止触摸穿透,
            });
            setTimeout(function() {
                wx.hideToast();
            }, 1000);
            return
        }
        util.request(getApp().api.greeting.command, { orderId: _this.data.orderId, command: _this.data.command }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                            giveType: 3
                        })
                        // _this.getGiftInfo(_this.data.orderId);
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    inputBlur: function(e) {
        var t = '{"' + e.currentTarget.dataset.name + '":"' + e.detail.value + '"}';
        this.setData(JSON.parse(t));
    },
    bindRegionChange: function(e) {
        this.setData({
            pickerColor: '#AAACAA',
            region: e.detail.value,
            code: e.detail.code
        })
    },
    confirmReceive: function() {
        const _this = this;
        const openid = wx.getStorageSync('openid');
        if (_this.data.giveType == 2 || _this.data.giveType == 3) {
            if (!_this.data.name) {
                wx.showToast({
                    title: '请填写收货人姓名', //提示的内容,
                    icon: "none", //图标,
                    //duration: 1500, //延迟时间,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1000);
                return
            }
            if (!_this.data.mobile) {
                wx.showToast({
                    title: '请填写手机号码', //提示的内容,
                    icon: "none", //图标,
                    //duration: 1500, //延迟时间,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1000)
                return
            }
            if (_this.data.mobile.length != 11) {
                wx.showToast({
                    title: '请填写正确的手机号码', //提示的内容,
                    icon: "none", //图标,
                    //duration: 1500, //延迟时间,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1000)
                return
            }
            if (_this.data.region.length !== 3) {
                wx.showToast({
                    title: '请选择省市区', //提示的内容,
                    icon: "none", //图标,
                    //duration: 1500, //延迟时间,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1000);
                return
            }
            if (!_this.data.detail) {
                wx.showToast({
                    title: '请填写详细地址', //提示的内容,
                    icon: "none", //图标,
                    //duration: 1500, //延迟时间,
                    mask: false, //显示透明蒙层，防止触摸穿透,
                });
                setTimeout(function() {
                    wx.hideToast();
                }, 1000);
                return
            }
        }

        var address = [{
            deliveryName: _this.data.name,
            deliveryMobile: _this.data.mobile,
            province: _this.data.region[0],
            city: _this.data.region[1],
            area: _this.data.region[2],
            town: '',
            street: _this.data.detail,
        }];
        util.request(getApp().api.greeting.receive, { address: address, orderId: _this.data.orderId, giveAway: openid }, 'POST').then(
            function(res) {
                if (res.code == 200) {
                    wx.redirectTo({
                        url: '/pages/exchange-suc/exchange-suc'
                    })
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        )
    },
    getGiftInfo: function(e) {
        const _this = this;
        util.request(getApp().api.greeting.orderInfo, { orderId: e }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        order: res.data,
                        audiosrc: res.data.giveSpeak
                    });
                    if (res.data.giveAway) {
                        _this.setData({
                            giveType: 1,
                            isReceived: !0
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },

    getCradUrl: function(e) {
        const _this = this;
        util.request(getApp().api.card.cardUrl, { id: e }).then(
            function(res) {
                if (res.code == 200) {
                    _this.setData({
                        cardPic: res.data.cardEnvelopeUrl,
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function() {
                        wx.hideToast();
                    }, 1500);
                    return
                }
            }
        );
    },
    toGive: function() {
        wx.redirectTo({
            url: '/pages/gift-goods-list/gift-goods-list'
        })
    },


    //初始化播放器，获取duration
    Initialization() {
        var t = this;
        if (this.data.audiosrc.length != 0) {
            //设置src
            innerAudioContext.src = this.data.audiosrc;
            //运行一次
            innerAudioContext.play();
            innerAudioContext.pause();
            innerAudioContext.onCanplay(() => {
                //初始化duration
                innerAudioContext.duration
                setTimeout(function() {
                    //延时获取音频真正的duration
                    var duration = innerAudioContext.duration;
                    var min = parseInt(duration / 60);
                    var sec = parseInt(duration % 60);
                    if (min.toString().length == 1) {
                        min = `0${min}`;
                    }
                    if (sec.toString().length == 1) {
                        sec = `0${sec}`;
                    }
                    t.setData({ audioDuration: innerAudioContext.duration, showTime2: `${min}:${sec}` });
                }, 1000)
            })
        }
    },
    loadaudio() {
        var that = this;
        //设置一个计步器
        this.data.durationIntval = setInterval(function() {
            //当歌曲在播放时执行
            if (that.data.playStatus == true) {
                //获取歌曲的播放时间，进度百分比
                var seek = that.data.audioSeek;
                var duration = innerAudioContext.duration;
                var time = that.data.audioTime;
                time = parseInt(100 * seek / duration);
                //当歌曲在播放时，每隔一秒歌曲播放时间+1，并计算分钟数与秒数
                var min = parseInt((seek + 1) / 60);
                var sec = parseInt(seek % 60);
                //填充字符串，使3:1这种呈现出 03：01 的样式
                if (min.toString().length == 1) {
                    min = `0${min}`;
                }
                if (sec.toString().length == 1) {
                    sec = `0${sec}`;
                }
                var min1 = parseInt(duration / 60);
                var sec1 = parseInt(duration % 60);
                if (min1.toString().length == 1) {
                    min1 = `0${min1}`;
                }
                if (sec1.toString().length == 1) {
                    sec1 = `0${sec1}`;
                }
                //当进度条完成，停止播放，并重设播放时间和进度条
                if (time >= 100) {
                    innerAudioContext.stop();
                    that.setData({ audioSeek: 0, audioTime: 0, audioDuration: duration, playStatus: false, showTime1: `00:00` });
                    return false;
                }
                //正常播放，更改进度信息，更改播放时间信息
                that.setData({ audioSeek: seek + 1, audioTime: time, audioDuration: duration, showTime1: `${min}:${sec}`, showTime2: `${min1}:${sec1}` });
            }
        }, 1000);
    },
    //播放、暂停按钮
    playAudio() {
        //获取播放状态和当前播放时间
        var playStatus = this.data.playStatus;
        var seek = this.data.audioSeek;
        innerAudioContext.pause();

        //  // 音频地址
        //     innerAudioContext.src = _this.data.audiosrc
        //         //在ios下静音时播放没有声音，默认为true，改为false就好了。
        //     innerAudioContext.obeyMuteSwitch = false

        //更改播放状态
        this.setData({ playStatus: !playStatus })
        if (playStatus) {
            //如果在播放则记录播放的时间seek，暂停
            this.setData({ audioSeek: innerAudioContext.currentTime });
        } else {
            //如果在暂停，获取播放时间并继续播放
            innerAudioContext.src = this.data.audiosrc;
            if (innerAudioContext.duration != 0) {
                this.setData({ audioDuration: innerAudioContext.duration });
            }
            //跳转到指定时间播放
            innerAudioContext.seek(seek);
            innerAudioContext.play();
        }
    },
    //拖动进度条事件
    sliderChange(e) {
        innerAudioContext.src = this.data.audiosrc;
        //获取进度条百分比
        var value = e.detail.value;
        this.setData({ audioTime: value });
        var duration = this.data.audioDuration;
        //根据进度条百分比及歌曲总时间，计算拖动位置的时间
        value = parseInt(value * duration / 100);
        //更改状态
        this.setData({ audioSeek: value, playStatus: true });
        //调用seek方法跳转歌曲时间
        innerAudioContext.seek(value);
        //播放歌曲
        innerAudioContext.play();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
})